//
//  TitleIncomeTypeCell.m
//  MoneyIndividual
//
//  Created by Le Anh Tai on 1/6/14.
//  Copyright (c) 2014 Le Anh Tai. All rights reserved.
//

#import "TitleIncomeTypeCell.h"

@implementation TitleIncomeTypeCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.textFieldMode = TextFieldInputModeTableView;
        
        _typeView = [[ExpenseIncomeTypeView alloc] init];
        NSMutableArray *incomeAccounts = [[NSMutableArray alloc] init];
        for (int i = 0; i <= TransactionTypeIncomeHui; i++)
        {
            [incomeAccounts addObject:[[CoreData shared].accounts objectAtIndex:i]];
        }
        
        _typeView.objects = incomeAccounts;
        _typeView.delegate = self;
        
        self.textField.clearButtonMode = UITextFieldViewModeNever;
    }
    
    return self;
}

- (void)setSelectedValue:(id)selectedValue
{
    [super setSelectedValue:selectedValue];
    
    self.textField.text = [selectedValue valueForKey:NAME_KEY];
}

#pragma mark - TextField delegate

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [super textFieldDidBeginEditing:textField];
    
    _typeView.selectedObject = self.selectedValue;
    
    [self.textField setInputView:_typeView];
}

#pragma mark - ExpenseIncomeTypeView delegate
- (void)table:(TableView *)view didSelectManageObject:(id)selectedObject
{
    self.selectedValue = selectedObject;
    [self.textField resignFirstResponder];
}
@end
